package demo.swing.administrator.panel.models;

import java.util.ArrayList;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

public class CityComboBoxModel implements ComboBoxModel<String> {

	List<String> data = new ArrayList<String>();
	ArrayList<ListDataListener> listeners = new ArrayList<ListDataListener>();
	int selected = 0;
	
	public void updateData(List<String> l){
		this.selected = 0;
		int left = data.size();
		data.addAll(l);
		int right = data.size() - 1;
		for(int i = 0; i < listeners.size(); i++){
			listeners.get(i).contentsChanged(new ListDataEvent(this, ListDataEvent.INTERVAL_ADDED, left, right));
		}		
	}
	
	public CityComboBoxModel(){
		data.add(new String("All city"));
	}
	
	@Override
	public int getSize() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public String getElementAt(int index) {
		// TODO Auto-generated method stub
		return data.get(index);
	}

	@Override
	public void addListDataListener(ListDataListener l) {
		// TODO Auto-generated method stub
		listeners.add(l);
	}

	@Override
	public void removeListDataListener(ListDataListener l) {
		// TODO Auto-generated method stub
			listeners.remove((Object)l);
	}

	@Override
	public void setSelectedItem(Object anItem) {
		// TODO Auto-generated method stub
		for(int i = 0; i < data.size(); i++){
			if (((Object)data.get(i)).equals(anItem)){
				selected = i;
			}
		}
	}

	@Override
	public Object getSelectedItem() {
		// TODO Auto-generated method stub
		if (selected < 0){
			return null;
		}else{
			return (Object)data.get(selected);	
		}
		
	}

}
